package com.duleme.servlet.order;

import com.duleme.common.Config;
import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.dao.OrderDao;
import com.duleme.entity.Delivery;
import com.duleme.entity.Order;
import com.duleme.entity.ShoppingCart;
import com.duleme.entity.User;
import com.duleme.service.CartService;
import com.duleme.service.ShopService;
import com.duleme.service.impl.CartServiceImpl;
import com.duleme.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "XinServlet",urlPatterns = "/send")
public class XinServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        User user = (User) request.getSession().getAttribute("user");
        /*User user = new User();
        user.setUserId(100020);*/
        int deliveryID = Integer.valueOf(request.getParameter("deliveryID"));
        double totalPrice = Double.valueOf(request.getParameter("totalPrice"));
        request.getSession().setAttribute("totalPrice",totalPrice);
        Delivery delivery = new Delivery();
        delivery.setDeliveryId(deliveryID);
        ShopService shopService = new ShopServiceImpl();
        List<Integer> list = shopService.getOrderIDByUser(user.getUserId(), 1);
        Boolean flag = true;
        for (int i:list){
            Boolean ist = new OrderDao().setAddress(i,deliveryID);
            System.out.println("orderID"+i);
            flag&=ist;
        }
        if (flag){
            String json = JsonUtils.result2Json("1");
            response.getWriter().write(json);
        }else {
            String json = JsonUtils.result2Json("0");
            response.getWriter().write(json);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        request.getSession().setAttribute("user",null);
        request.getSession().setAttribute("userInfo",null);
        request.getSession().setAttribute("shop",null);
        request.getSession().setAttribute("login",null);
        String json = JsonUtils.result2Json("1");
        response.getWriter().write(json);
    }
}
